package com.dlb.andorid.activity;

import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dlb.andorid.App;
import com.dlb.andorid.R;
import com.dlb.andorid.XContract;
import com.dlb.andorid.bean.ProductBean;
import com.dlb.andorid.bean.ServerResult;
import com.dlb.andorid.presenter.SearchPresenter;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonActivity;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends BaseCommonActivity implements XContract.ISearchView {

	SearchPresenter mPresenter;
	RecyclerView rv_home;

	View tv_search;
	EditText et_search;


	@Override
	protected BasePresenter getBasePresenter() {
		mPresenter = new SearchPresenter(this);
		return mPresenter;
	}

	@Override
	protected void initData() {

	}

	@Override
	protected int getTopViewId() {
		return 0;
	}

	@Override
	protected int getMainViewId() {
		return R.layout.activity_search;
	}

	@Override
	protected int getBottomViewId() {
		return 0;
	}

	@Override
	protected String getTitleTxt() {
		return "搜索";
	}

	@Override
	public String getPageName() {
		return null;
	}

	BaseQuickAdapter<ProductBean, BaseViewHolder> mHomeAdapter;
	List<ProductBean> mProducts = new ArrayList<>();

	@Override
	protected void initViews() {
		super.initViews();

		tv_search = findViewById(R.id.tv_search);
		tv_search.setOnClickListener(this);

		et_search = findViewById(R.id.et_search);

		rv_home = findViewById(R.id.rv_home);

		mHomeAdapter = new BaseQuickAdapter<ProductBean, BaseViewHolder>(R.layout.list_item_home, mProducts) {
			@Override
			protected void convert(BaseViewHolder helper, ProductBean item) {

				mImageLoader.loadUrl(item.getPic_cover(), (ImageView) helper.getView(R.id.iv_icon));
				helper.setText(R.id.tv_name, item.getName() + " " + item.getType_name());
				helper.setText(R.id.tv_price, item.getSale_price() + "");
				String op = item.getOrdinary_price() + "";
				SpannableString sp = new SpannableString(op);
				sp.setSpan(new StrikethroughSpan(), 0, op.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
				((TextView) helper.getView(R.id.tv_price_old)).setText(sp);
				String[] tags = item.getTag_name().split(",");

				if (tags.length == 1) {
					helper.getView(R.id.tv_tag2).setVisibility(View.INVISIBLE);
					if (tags[0] == null || item.getTag_name().isEmpty()) {
						helper.getView(R.id.tv_tag1).setVisibility(View.INVISIBLE);
					} else {
						helper.setText(R.id.tv_tag1, tags[0]);
					}

				} else {

					if (tags[0] == null || item.getTag_name().isEmpty()) {
						helper.getView(R.id.tv_tag1).setVisibility(View.INVISIBLE);
					} else {
						helper.setText(R.id.tv_tag1, tags[0]);
					}

					if (tags[1] == null || item.getTag_name().isEmpty()) {
						helper.getView(R.id.tv_tag2).setVisibility(View.INVISIBLE);
					} else {
						helper.setText(R.id.tv_tag2, tags[1]);
					}
				}

				helper.addOnClickListener(R.id.iv_car);

			}
		};
		rv_home.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));
		rv_home.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		rv_home.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		rv_home.setHasFixedSize(true);

		//解决更新 闪烁
		rv_home.setItemAnimator(null);
		rv_home.setNestedScrollingEnabled(false);
		rv_home.setAdapter(mHomeAdapter);

		mHomeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				Bundle bundle = new Bundle();
				bundle.putLong("id", mProducts.get(position).getId());
				go2Activity(ProductDetailActivity.class, bundle, false);
			}
		});

		mHomeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
			@Override
			public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
				//toastMsg(""+mProducts.get(position).getId());
				Log.e("kwan", "car:" + mProducts.get(position).getId());
				if (view.getId() == R.id.iv_car) {
					Log.e("kwan", "car:" + mProducts.get(position).getId());
					showProgress("");
					mPresenter.addCar(mProducts.get(position).getId(), App.mUserInfo.getUid(), 1, mProducts.get(position).getType_id(), mProducts.get(position).getSale_price());
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);

		if (v == tv_search) {

			String str = et_search.getText().toString();
			if (str.isEmpty()) {
				toastMsg("请输入内容");
				return;
			}

			showProgress("");
			mPresenter.search(str, 1);

		}

	}

	@Override
	public void getSearchProductsSuccess(List<ProductBean> data) {
		dismissProgress();
		if (data != null && data.size() > 0) {
			mProducts.clear();
			mProducts.addAll(data);
			mHomeAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void addCarSuccess(ServerResult data) {
		dismissProgress();
		toastMsg(data.getMsg());
	}
}
